from fastapi import Depends
from typing import Annotated
from sqlmodel import Session, create_engine, SQLModel
from config import Config
from models import *

connect_args = {"check_same_thread": False}  # 解决多线程问题

# 连接数据库
engine = create_engine(
    url=Config.SQL_DATABASE_URI,
    pool_pre_ping=True
)  # 连接mysql


# 初始化创建所有表
def create_db_and_tables():
    SQLModel.metadata.create_all(engine)

# 删除所有表
def drop_db_and_tables():
    SQLModel.metadata.drop_all(engine)


def get_session():
    with Session(engine) as session:
        yield session


SessionDep = Annotated[Session, Depends(get_session)]



